package com.otojob.utils.interceptor;

import com.otojob.utils.JwtUtils;
import com.otojob.utils.OtoException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description
 * @Author: TingFeng
 * @CreateTime: 2022/4/11  21:13
 */
@Component
@Slf4j
public class LoginInterceor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object hander) throws Exception {
        response.setCharacterEncoding("UTF-8");
        //Authorization参数是前台放在headler中的key
        String token = request.getHeader("token");
        if(token != null){
            //判断token是否有效
            boolean flag = JwtUtils.checkToken(request);
            if(flag){
                return true;
            }
        }
//        System.out.println("token认证失败，请登录...");//跳转
        log.info("token认证失败，请登录...");
        request.setAttribute("msg","没有权限");
        //请求转发到登录界面
//        request.getRequestDispatcher("/wx/login").forward(request,response);
        return false;
    }
}


